<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\FhAbout;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Http\Controllers\AdminController;

class FhAboutController extends AdminController
{

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FhAbout(), function (Grid $grid) {
            $grid->column('img')->image('',100,60);
            $grid->column('about');
            $grid->column('team');
            $grid->column('culture');
            $grid->actions(function ($actions) {
                // 隐藏显示按钮
                $actions->disableView();
                $actions->disableDelete();
            });
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FhAbout(), function (Show $show) {
            $show->field('id');
            $show->field('img');
            $show->field('about');
            $show->field('honor');
            $show->field('team');
            $show->field('people');
            $show->field('company');
            $show->field('culture');
            $show->field('culture_img');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $builder = FhAbout::with('list');
        return Form::make($builder, function (Form $form) {
            // $form->disableListButton();
           
            $form->display('id');                
            $form->textarea('about')->required();
            $form->image('img')->required();
            $form->textarea('team')->required();
            $form->multipleImage('people')->required()->move('images/'.date("Ymd",time()))->saving(function ($value) {
                // 转化成json字符串保存到数据库
                    return implode(',',$value);
            });
            $form->multipleImage('company')->required()->move('images/'.date("Ymd",time()))->saving(function ($value) {
                // 转化成json字符串保存到数据库
                    return implode(',',$value);
            });
            $form->textarea('culture')->required();
            $form->multipleImage('culture_img')->required()->move('images/'.date("Ymd",time()))->saving(function ($value) {
                // 转化成json字符串保存到数据库
                    return implode(',',$value);
            });
            $form->hasMany('list', '资质证书', function (Form\NestedForm $form) {
                $form->text('name','证书名称')->required();
                $form->image('imgst','证书图片')->thumbnail('small',200,300)->move('images/'.date("Ymd",time()))->required();
            })->required();
            

            $form->saved(function (Form $form, $result) {
                $ids = $form->model()->id; 
                if($form->input('list')){
                    $datas = [];
                    foreach ($form->input('list') as $vals)
                    {
                        $thumb=explode(".", $vals['imgst']);
                        $val['name'] = $vals['name'];
                        $val['imgst'] = $vals['imgst'];
                        $val['thumb'] = $thumb[0].'-small.'.$thumb[1];
                        $val["p_id"] = $ids;
                        $datas[] = $val;
                    }
                  $form->list = $datas;
                }
                return;
            });




            $form->display('created_at');
            $form->display('updated_at');
        
            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList('true','');
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
            $form->footer(function ($footer) {
                // 去掉`查看`checkbox
                $footer->disableViewCheck();
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });
        });
    }
}
